package com.emm.yixun.mobile.ui.notice;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.adapter.NoticeAdaptercompany;
import com.emm.yixun.mobile.application.EmmApplication;
import com.emm.yixun.mobile.model.GetNoticeList;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.HashMap;
import java.util.Map;

import xf.tools.Loading;

public class ItemFragment extends Fragment {

    private static final String TAG = "ItemFragment";

    GetNoticeList getnoticelist;

    String title;
    String ID;
    NoticeAdaptercompany adapternotice;

    ListView list_view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View contextView = inflater.inflate(R.layout.fragment_item, container, false);

        // 获取传值
        Bundle mBundle = getArguments();
        title = mBundle.getString("arg");
        ID = mBundle.getString("ID");

        Log.v(TAG, "title:" + title + "--ID:" + ID);

        list_view = (ListView) contextView.findViewById(R.id.list_view);

        initBtn(getActivity(), ID);
        return contextView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    private void initBtn(Context context, String ID) {
        getNoticeList(ID);
        list_view.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                getnoticelist.getNoticeList().get(position).setIsRead("已读");
                adapternotice.notifyDataSetChanged();

                Intent intent = new Intent();
                intent.putExtra("TITLE", title);
                intent.putExtra("noticeId", getnoticelist.getNoticeList().get(position).getNoticeId());
                intent.setClass(getActivity(), NoticeTextContentActivity.class);
                startActivity(intent);
            }
        });

    }

    /**
     * <p>
     * <b>Title：</b> 获取公告详细列表
     * </p>
     * <p>
     * <b>Description：</b>
     * </p>
     *
     * @param ID
     * @author tangxin
     * @date 2016年12月21日 下午3:44:08
     */
    private void getNoticeList(String ID) {
        EmmApplication.updateUrl("getNoticeList");
        Map<String, String> map = new HashMap<String, String>();
        map.put("version", EmmApplication.getPackageManager(5));
        map.put("merchantId", EmmApplication.getData(EmmApplication.merchantId));
        map.put("token", EmmApplication.getToken());
        map.put("userId", EmmApplication.getUserId());
        map.put("terminalCode", EmmApplication.getPackageManager(2));
        map.put("projectCode", EmmApplication.getProjectCode());
        if ("999".equals(ID)) {
            map.put("noticeCategoryId", "");
        } else {
            map.put("noticeCategoryId", ID);
        }
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("params", map);
        JSONObject json = new JSONObject(paramsMap);
        String jsonstr = json.toString();
        RequestParams params = new RequestParams();
        params.put("body", jsonstr);
        Log.v("getNoticeList-->post", EmmApplication.urlStr + "?body=" + jsonstr);
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        asyncHttpClient.post(EmmApplication.urlStr, params, new AsyncHttpResponseHandler() {
            @Override
            public void onFailure(Throwable error, String content) {
                super.onFailure(error, content);
                Loading.hideDialogForLoading();
                Log.v(TAG, "请求结束error:" + error + "==" + content);
            }

            @Override
            public void onFinish() {
                Log.v(TAG, "请求结束");
                super.onFinish();
                Loading.hideDialogForLoading();
            }

            @Override
            public void onStart() {
                super.onStart();
                Loading.hideDialogForLoading();
                Log.v(TAG, "请求开始");
            }

            @Override
            public void onSuccess(String content) {
                super.onSuccess(content);
                if (content == null) {
                    return;
                }
                Log.v(TAG, "content:" + content);
                JSONObject jsonObj = JSONObject.parseObject(content);
                JSONObject resData = (JSONObject) jsonObj.get("resData");
                if (resData == null) {
                    Log.v(TAG, "信息返回值为空");
                    return;
                }

                getnoticelist = JSONObject.parseObject(resData.toString(), GetNoticeList.class);
                if ("1".equals(getnoticelist.getResult())) {
                    Log.v("获取成功", "");
                    if (null != getnoticelist && null != getnoticelist.getNoticeList()) {
                        adapternotice = new NoticeAdaptercompany(getActivity(), getnoticelist.getNoticeList());
                        list_view.setAdapter(adapternotice);
                    } else {
                        Log.v(TAG, "公告列表为空");
                    }
                } else {
                    EmmApplication.T(getnoticelist.getErrorMsg().toString());
                    try {
                        Log.v("获取失败", "getNoticeList-->errorCode:" + getnoticelist.getErrorCode().toString()
                                + "errorMsg:" + getnoticelist.getErrorMsg().toString());
                    } catch (NullPointerException e) {
                        Log.v(TAG, "getNoticeList-->客户详情，错误信息获取空指针异常");
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}